//Accepted
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <iostream>
#include <list>
#include <deque>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef set<int> si;

int n, k, i, x;
string a;
vector< pair<int,string> > v;

bool compare(pair<int,string> a, pair<int,string> b){
	return a.first<b.first;
}

int main (){
	//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);
	scanf("%d",&n);
	for(k=1;k<=n;++k){
		v.clear();
		for(i=0;i<10;++i){
			cin>>a>>x;
			v.push_back(make_pair(-x, a));
		}
		sort(v.begin(),v.end(), compare);
		printf("Case #%d:\n%s\n",k,v[0].second.c_str());
		i=1;
		x=v[0].first;
		while(v[i].first==x)printf("%s\n",v[i++].second.c_str());
	}
	return 0;
}
